﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

#if USE_OPENJTALK
namespace NicoView.Model.Platform.Speech.OpenJTalk
{
    public sealed class OpenJTalkSpeacherProvider:SpeacherProviderBase
    {
        public OpenJTalkSpeacherProvider(string name ,string dicotionaryDir,string synthesizerDirectory)
        {
            DictionaryDirectory = dicotionaryDir;
            SynthesizerDirectory = synthesizerDirectory;
            _name = name;

        }

        public string DictionaryDirectory
        {
            get;
            private set;
        }

        private static SynchronizationContext _context = SynchronizationContext.Current;

        public string SynthesizerDirectory
        {
            get;
            private set;
        }

        private string _name;



        public override string Name
        {
            get { return _name; }
        }

        public override ISpeacher GetInstance()
        {
            if (Speacher == null)
            {
                _context.Send((s) =>
                {
                    Speacher = new OpenJTalkSpeacher(this.Name, this.DictionaryDirectory, SynthesizerDirectory, RegexLearnDictionary,LearnDictionary);
                },null);
            }
            return Speacher;
        }


        private static RegexLearnDictionary RegexLearnDictionary { get; set; }

        private static LearnDictionary LearnDictionary { get; set; }

        public static ICollection<SpeacherProviderBase> GetInstalledProvider(RegexLearnDictionary regexDictionary,LearnDictionary dictionary)
        {
            if (LearnDictionary == null) LearnDictionary = dictionary;
            if (RegexLearnDictionary == null) RegexLearnDictionary = regexDictionary;
            List<SpeacherProviderBase> list = new List<SpeacherProviderBase>();
            
            foreach(var folder in  _getInstalledSynthesizer(@".\Voice\Synthesizer").GetAwaiter().GetResult())
            {
                list.Add(new OpenJTalkSpeacherProvider(folder.Name, _getDictionaryDir( @".\Voice\Dictionary").GetAwaiter().GetResult(), folder.Path));
            }

            return list;

        }


        private static async Task<IEnumerable<CommonStorageFolder>> _getInstalledSynthesizer(string dir)
        {
            var folder = await CommonStorageFolder.GetFolderFromPathAsync(dir);
            return await folder.GetFoldersAsync();

        }


        private static async Task<string> _getDictionaryDir(string dir)
        {
            var folder = await CommonStorageFolder.GetFolderFromPathAsync(dir);
            return folder.Path;
        }




        public override SpeacherProviderBase Copy()
        {
            return new OpenJTalkSpeacherProvider(this.Name, this.DictionaryDirectory, this.SynthesizerDirectory);
        }
    }
}
#endif